<?php
// +----------------------------------------------------------------------
// | CRMEB [ CRMEB赋能开发者，助力企业发展 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2016~2023 https://www.crmeb.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed CRMEB并不是自由软件，未经许可不能去掉CRMEB相关版权
// +----------------------------------------------------------------------
// | Author: CRMEB Team <admin@crmeb.com>
// +----------------------------------------------------------------------
namespace app\adminapi\controller\v1\marketing\score;

use app\adminapi\controller\AuthController;
use app\Request;
use app\services\activity\score\StoreScoreServices;
use think\facade\App;

/**
 * 积分商城管理
 * Class StoreCombination
 * @package app\admin\controller\store
 */
class StoreScore extends AuthController
{
    /**
     * StoreIntegral constructor.
     * @param App $app
     * @param StoreScoreServices $services
     */
    public function __construct(App $app, StoreScoreServices $services)
    {
        parent::__construct($app);
        $this->services = $services;
    }

    /**
     * 积分商品列表
     * @return mixed
     */
    public function index()
    {
        $where = $this->request->getMore([
            ['name', ''],
            ['type', ''],
            ['is_show', '']
        ]);
        $where['is_del'] = 0;
        if (!$where['name']) {
            unset($where['name']);
        }
        if ($where['type'] == '') {
            unset($where['type']);
        }
        if ($where['is_show'] == '') {
            unset($where['is_show']);
        }
        $list = $this->services->systemPage($where);
        return app('json')->success($list);
    }

    /**
     * 保存商品
     * @param $id
     * @return \think\Response
     */
    public function save($id)
    {
        $data = $this->request->postMore([
            ['type', 0],//0实物 1虚拟
            ['image', ""],// 图片
            ['name', ""],//商品名称
            ['code', ""],// 编码
            ['price', "0"],// 价格
            ['worth', 0],// 商品价值(优惠券面额)
            ['spec_type', 0],//单多规格  0单 1多
            ['items', []],//规格
            ['attrs', []],//规格
            ['freight', 0],//运费设置
            ['is_show', 1],//是否上架
            ['sort', 0],//排序
            ['sales', 0],//销量
            ['stock', 0],//(虚拟)库存
            ['stock_type', 0],//(虚拟)库存类型 1无限制 2有限制
            ['coupon_type', 0],//(虚拟)适用商品 1通用 0部分
            ['products', []],//(虚拟)可用商品
            ['use_min_price', '0.00'],//(虚拟)消费多少可用
            ['validity_type', 0],//(虚拟)有效期 0永久 1时段
            ['validity_date_start', 0],//(虚拟)有效期起
            ['validity_date_end', 0],//(虚拟)有效期末
            ['instructions', ''],//使用说明
            ['detail_pc', ''],//商品详情pc
            ['detail_h5', ''],//商品详情h5
        ]);
        $this->validate($data, \app\adminapi\validate\marketing\StoreScoreValidate::class, 'save');

        if ($id) {
            $integral = $this->services->get((int)$id);
            if (!$integral) {
                return app('json')->fail(100026);
            }
        }
        $this->services->save($id, $data);
        return app('json')->success(100000);
    }


    /**
     * 批量添加商品
     * @return mixed
     */
    public function batch_add()
    {
        $data = $this->request->postMore([
            ['attrs', []],
            [['is_show', 'd'], 0]
        ]);
        $this->services->saveBatchData($data);
        return app('json')->success(100000);
    }

    /**
     * 详情
     * @param $id
     * @return mixed
     */
    public function read($id)
    {
        $info = $this->services->getInfo($id,"");
        return app('json')->success(compact('info'));
    }

    /**
     * 修改状态
     * @param $id
     * @param $status
     * @return mixed
     */
    public function set_show($id, $is_show)
    {
        $this->services->update($id, ['is_show' => $is_show]);
        return app('json')->success(100014);
    }

    /**
     * 删除指定资源
     *
     * @param int $id
     * @return \think\Response
     */
    public function delete()
    {
        $param = $this->request->postMore([
            ['id', 0],
        ]);
        if (!$param['id']) return app('json')->fail(100100);
        $this->services->update($param['id'], ['is_del' => 1]);
        return app('json')->success(100002);
    }

}
